package com.metaapp.demo.custom.recyclerview;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public abstract class CommonAdapter<T> extends RecyclerView.Adapter<ViewHolder> {


    public Context mContext;
    /**
     * 数据list
     */
    private ArrayList<T> mList = new ArrayList<>();

    /**
     * 多布局接口
     */
    private MultipleType<T> mMultipleType = null;

    /**
     * 单一布局资源id
     */
    private int layoutId;

    /**
     * 点击事件
     */
    public OnItemClickListener<T> onItemClickListener = null;

    /**
     * 按钮或者TV的点击事件
     */
    public OnTypeClickListener<T> onTypeClickListener = null;

    private int itemNumber = 0;

    /**
     * 多种布局
     *
     * @param context Context
     * @param list ArrayList
     * @param type MultipleType
     */
    public CommonAdapter(Context context, ArrayList<T> list, MultipleType<T> type) {
        this.mContext = context;
        this.mList = list;
        this.mMultipleType = type;
    }

    public CommonAdapter(Context context, MultipleType<T> type) {
        this.mContext = context;
        this.mMultipleType = type;
    }

    /**
     * 单一布局
     *
     * @param context Context
     * @param list ArrayList
     * @param layoutId int
     */
    public CommonAdapter(Context context, ArrayList<T> list, int layoutId) {
        this.mContext = context;
        this.mList = list;
        this.layoutId = layoutId;
    }

    public CommonAdapter(Context context, int layoutId) {
        this.mContext = context;
        this.layoutId = layoutId;
    }
    //高度显示几个item
    public CommonAdapter(Context context, int layoutId, int itemNumber){
        this.mContext = context;
        this.layoutId = layoutId;
        this.itemNumber = itemNumber;
    }

    //只适用测量固定的item，其他不适用
    private int itemHeight;

    public int getItemHeight() {
        return itemHeight;
    }

    private int flag = 0;
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater mInflater = LayoutInflater.from(mContext);
        //是多布局
        if (mMultipleType != null) {
            layoutId = viewType;
        }
        View view = mInflater.inflate(layoutId, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        //控制一行显示几个item

        if (itemNumber != 0 && flag == 0) {
            int height = parent.getHeight();
            ViewGroup.LayoutParams layoutParams = viewHolder.itemView.getLayoutParams();
            layoutParams.height = height / itemNumber;
            itemHeight = height / itemNumber;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        //把必要的数据传递出去
        bindData(holder, mList.get(position), position);
    }
    @Override
    public int getItemViewType(int position) {
        //多布局问题
        return mMultipleType != null ? mMultipleType.getLayoutId(mList.get(position), position) : position;
    }

    @Override
    public int getItemCount() {
        return mList != null ? mList.size() : 0;
    }

    /**
     * 将必要参数传递出去
     *
     * @param holder ViewHolder
     * @param data T
     * @param position int
     */
    protected abstract void bindData(ViewHolder holder, T data, int position);


    public void setOnItemClickListener(OnItemClickListener<T> onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnTypeClickListener(OnTypeClickListener<T> onTypeClickListener) {
        this.onTypeClickListener = onTypeClickListener;
    }

    /**
     * 添加数据.适用于下拉刷新和第一次添加数据
     *
     * @param newDate ArrayList
     */
    public void setData(List<T> newDate) {
        this.mList.clear();
        this.mList.addAll(newDate);
        notifyDataSetChanged();
    }

    /**
     * 设置加载更多数据
     *
     * @param addDate ArrayList
     */
    public void setLoadMoreDate(List<T> addDate) {
        this.mList.addAll(addDate);
        Log.e(".....", "setLoadMoreDate: "+mList.size() );
        notifyItemInserted(this.mList.size());
    }

    public void setData(T t) {
        this.mList.add(t);
        notifyItemInserted(this.mList.size());
    }

    public Context getContext() {
        return mContext;
    }

    public ArrayList<T> getListDate() {
        return mList;
    }

}
